<template>
  <div>
    <FormPanel ref="from" :list="formList" @formChange="changeHandle" :isSubmitBtn="false" :collapse="false" labelWidth="110" />
    <div style="height:47px;"></div>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDrawer(1)">关 闭</el-button>
      <multiuse-button type="primary" :click="closeDrawer.bind(this, 2)">保 存</multiuse-button>
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { getFundsType, addSave,editSave } from '@/api/omFinance/RebateManage/RebateChilrden/index.js';
export default {
  props: {
    rowData: {
      type: Object,
      default: () => ({})
    },
    type: {
      type: String,
      default: 'add'
    }
  },
  data() {
    this.saveData = null;
    return {
      formList: this.createdFrom(),
      powerType: '',
      powerTypeList: []
    };
  },
  created() {
    this.getAccount();
    debugger
    if(this.rowData!=null){
        if (this.type === 'edit') {
          this.formList.forEach(x => {
          x.initialValue = this.rowData[x.fieldName];
          if(x.fieldName==="isValid"||x.fieldName==="rebateSubprojectsName"){
            x.disabled=false;
          }else{
            x.disabled=true;
          }

        });
        }
    }
  },
  mixins: [dictionary],
  methods: {
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '返利类型',
          fieldName: 'accountTypeId',
          placeholder: '请选择',
          itemList: [],
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          change: val => {
            if (val) {
              this.powerTypeList.map(x => {
                if (x.id == val) {
                  this.powerType = x.powerType;
                }
              });
              return;
            }
            this.powerType = '';
          }
        },
        {
          type: 'INPUT',
          label: '返利子项目代码',
          fieldName: 'rebateSubprojectsCode',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '返利子项目名称',
          fieldName: 'rebateSubprojectsName',
          placeholder: '请输入',
          rules: [{ required: true, message: '请输入', trigger: 'blur' }]
        },
        {
          type: 'INPUT',
          label: '排序值',
          fieldName: 'num',

          placeholder: '请输入',
          rules: [
            { required: true, message: '请输入', trigger: 'blur' },
            {
              validator: function(rule, value, callback) {
                if (value === undefined || value === '' || value === null) {
                  callback();
                }
                var g = /^[1-9]\d*$/;
                if (g.test(value)) {
                  callback();
                }
                callback(new Error('只能输入正整数'));
              },
              trigger: 'blur'
            }
          ]
        },
        {
          type: 'SELECT',
          label: '是否有效',
          rules: [{ required: true, message: '请选择', trigger: 'change' }],
          fieldName: 'isValid',
          initialValue:10011001, 
          placeholder: '请选择...',
          itemList: this.createDictList('1001')
        },
        {
          type: 'SELECT',
          label: '是否可以红冲',
          fieldName: 'isRed',
          placeholder: '请选择',
          hidden:true,
          itemList: [{text:'是',value:1},{text:'否',value:0}],
          rules: [{ required: false, message: '请选择', trigger: 'change' }]
        }
      ];
    },
    changeHandle(val) {
      this.saveData = val;
    },
    // 加载资金类型
    async getAccount() {
      let res = await getFundsType({ fundsType: 1 });
      this.formList.find(x => x.fieldName == 'accountTypeId').itemList = res.data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.powerTypeList = res.data;
    },
    async closeDrawer(type) {
      if (type == 1) {
        this.$emit('close');
      } else {
        this.$refs.from.SUBMIT_FORM();
        if (this.saveData) {
          this.saveData.powerType = this.powerType;
          let json = this.saveData;
          console.log('json',json)
        let res = null;
        if (this.type === 'edit') {
            json.id = this.rowData.id;
            res = await editSave(json);
        }else{
            res = await addSave(json);
        }
         
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '保存成功',
              type: 'success'
            });
            this.$emit('close', json);
          }
        }
      }
    }
  }
};
</script>
